<script setup>
import { ref } from 'vue'
import { Login, getUserInfo } from '../../services/user'
import { useRouter } from 'vue-router'

const router = useRouter()

const form = ref({
  phone: '',
  password: '',
})

const onSubmit = () => {
  console.log(form.value)
  // 登录逻辑
  Login(form.value)
    .then((res) => {
      console.log('完整的 API 响应:', res) // 打印 API 响应

      if (res.code === 200) {
        console.log(res.data)
        uni.setStorageSync('uid', res.obj.token) // 确保 token 被存储
        console.log('存储的 uid:', uni.getStorageSync('uid'))
        uni.setStorageSync('user', res.obj.user)
        console.log('存储的 user:', uni.getStorageSync('user'))
        uni.setStorageSync('userInformation', res.obj.userInformation)
        console.log('存储的 userInformation:', uni.getStorageSync('userInformation'))

        // 跳转后强制刷新页面
        router.push('/').then(() => {
          window.location.reload() // 强制刷新页面
        })
      } else {
        console.error('登录失败:', res.data ? res.data.message : '未知错误')
      }
    })
    .catch((error) => {
      console.error('请求错误:', error)
    })
}

function goToRegister() {
  // 使用 navigateTo 导航到注册页面
  uni.navigateTo({
    url: '/pages/res/res', // 确保路径正确，且不包含 .vue
  })
}

// 可选验证函数
const validatePhone = () => {
  // 可添加手机验证逻辑
}

const validatePassword = () => {
  // 可添加密码验证逻辑
}
</script>

<template>
  <view class="login-container">
    <view class="logo">
      <image src="https://zhengxin-pub.cdn.bcebos.com/mark/5b7250fd09b617c8dcc323cab6f58693.jpg" />
    </view>
    <view class="login-form">
      <input
        v-model="form.phone"
        class="input"
        type="text"
        placeholder="请输入手机号码"
        @blur="validatePhone"
      />
      <input
        v-model="form.password"
        class="input"
        type="password"
        placeholder="请输入密码"
        @blur="validatePassword"
      />
      <button @tap="onSubmit" class="button">登录</button>
      <view class="links">
        <text @click="goToRegister">没有账号？先注册</text>
      </view>
    </view>
  </view>
</template>
<style lang="scss">
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;

  .logo {
    margin-bottom: 30px;
  }

  .login-form {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 400px;

    .input {
      width: 100%;
      height: 45px;
      margin-bottom: 20px;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 5px;
      font-size: 16px;
    }

    .button {
      height: 50px;
      background-color: #28bb9c;
      color: #fff;
      border: none;
      border-radius: 10px;
      font-size: 20px;
      cursor: pointer;

      line-height: 40px; /* 将 line-height 设置为与按钮高度相等，文本垂直居中 */
      padding-top: 5px; /* 通过增加 padding-top 让文字稍微往下移动 */
      text-align: center; /* 确保文字在按钮内居中对齐 */
    }

    .links {
      display: flex;
      justify-content: center; /* 修改为居中对齐 */
      margin-top: 10px;
      font-size: 14px;
      color: #007aff;

      text {
        cursor: pointer;
      }
    }
  }
}
</style>
